package com.intel.factorytest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

public class TestShell {
    
    private String sysTestFile ="/data/data/com.intel.factorytest/sys_test.txt"; 
    private String TAG ="TestShell"; 
    private String command;    
    public boolean mCatchException = false;

    public TestShell()
    {
    }

    public void callShellCommand(String cmd){
        mCatchException = false;
        Log.e(TAG,"########### cmd ="+cmd);
        if (cmd.contains("cat")) {
        command = cmd + sysTestFile;
        }else {
        command = cmd;
        }
        Log.e(TAG,"********** command ="+command);
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"sh","-c",command});
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String tmp = "";
            while((tmp=br.readLine())!=null){
                Log.i("yjc",tmp);
            }
            br.close();

            InputStream eis = p.getErrorStream();
            InputStreamReader eisr = new InputStreamReader(eis);
            BufferedReader ebr = new BufferedReader(eisr);

            while((tmp=ebr.readLine())!=null){
                Log.e("yjc",tmp);
                if(tmp!= null && tmp.contains("No such")) {
                    Log.e("yjc","mCatchException");
                    mCatchException = true;
                }
            }
            ebr.close();

        } catch (IOException e) {
            mCatchException = true;
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
